<?php

namespace App\Http\Controllers\Basic;

use App\Helpers\Code;
use App\Models\Basic\Region;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class RegionController extends Controller
{

    /**
     * RegionController constructor.
     *
     * @author lql
     */
    public function __construct()
    {
        $this->model = new Region();

        $this->_validators();
    }

    /**
     * 添加大区
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function addRegion(Request $request)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['created_by', 'updated_by', 'data_scope']));

            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $this->result['data'] = $this->model->addRegion($data);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑大区
     * @param Request $request
     * @param $id
     * @return array
     *
     * @author lql
     */
    public function editRegion(Request $request, $id)
    {
        try {
            $data = $request->only(array_diff($this->model->getFillable(), ['created_by', 'updated_by', 'data_scope']));

            $rules = [];
            foreach ($data as $key => $value) {
                switch ($key) {
                    case 'title':
                        $rules['title'] = ['bail', 'required', 'max:100', Rule::unique('regions')->ignore($id)];
                        break;
                    case 'code':
                        $rules['code'] = ['bail', 'required', 'max:100', Rule::unique('regions')->ignore($id)];
                        break;
                    default:
                        $rules[$key] = $this->rules[$key];
                }
            }

            $validator = Validator::make($data, $rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editRegion($data, $id);
            $this->checked($model, [
                'null' => 'common.recored_not_found',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取大区全部
     * @return array
     *
     * @author lql
     */
    public function getRegionListAll()
    {
        try {
            $this->result['data'] = $this->model->getRegionListAll();

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取大区列表
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function getRegionList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);

            $limit = (int)$request->input('limit');

            $sorter = $this->handleSort($request, $this->model->sorters);

            $defaultSorter = $request->input('sorter') ?? 'asc';

            return preprocess($this->model->getRegionList($limit, $sorter, $search, $defaultSorter));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取大区树状结构
     * @return array
     *
     * @author lql
     */
    public function getRegionTree()
    {
        try {
            $this->result['data'] = $this->model->getRegionTree();

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'title' => 'bail|required|max:100|unique:regions',
            'code' => 'bail|required|max:100|unique:regions',
            'description' => 'bail|nullable|max:200',
            'rank' => 'bail|nullable|numeric',
            'state' => 'bail|required|in:0,1',
            'charged_by' => 'bail|nullable|exists:users,id',
        ];

        $this->messages = [
            'title.required' => trans('basic.title_required'),
            'title.unique' => trans('basic.title_unique'),
            'title.max' => trans('basic.title_max'),
            'code.required' => trans('basic.code_required'),
            'code.unique' => trans('basic.code_unique'),
            'code.max' => trans('basic.code_max'),
            'description.max' => trans('basic.description_max'),
            'rank.numeric' => trans('basic.rank_numeric'),
            'state.required' => trans('basic.state_required'),
            'state.in' => trans('basic.state_in'),
            'charged_by.exists' => trans('basic.charged_by_exists'),
        ];
    }

}
